﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.AspNetCore.Mvc.Extensions;
using FwProject.Controllers;
using FwProject.Core.Restaurant;
using FwProject.Core.SettlementInfoModel;
using FwProject.Distribution;
using FwProject.Distribution.Dto;
using FwProject.ManagerConfig;
using FwProject.Settlement;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FwProject.Web.Mvc.Controllers
{
    [AbpMvcAuthorize]
    public class ReceivableController : FwProjectControllerBase
    {
        private readonly IDistributionAppService _distributionAppService;
        private readonly ISettlementInfoAppService _settlementInfoAppService;
        private readonly IDistributionLineAppService _distributionLineAppService;
        private readonly IConfigInfoAppService _configInfoAppService;
        private readonly IDistributionDetailsAppService _distributionDetailsAppService;
        private readonly IHostingEnvironment _env;

        public ReceivableController(IDistributionAppService distributionAppService, IHostingEnvironment env, ISettlementInfoAppService settlementInfoAppService, IDistributionLineAppService distributionLineAppService, IConfigInfoAppService configInfoAppService, IDistributionDetailsAppService distributionDetailsAppService)
        {
            _distributionAppService = distributionAppService;
            _settlementInfoAppService = settlementInfoAppService;
            _distributionLineAppService = distributionLineAppService;
            _configInfoAppService = configInfoAppService;
            _distributionDetailsAppService = distributionDetailsAppService;
            _env = env;
        }
        public async Task<ActionResult> Index(int pageIdx, int size, DateTime? beginTime, DateTime? endTime, DateTime? beginDate, DateTime? endDate, string RestaurantName, string DriverName, RestaurantPayStatus? RestaurantPayStatus, BalanceWayEnum? BalanceWayEnum)
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 20;

            if (Request.IsAjaxRequest())
            {
                var model = await _distributionAppService.GetReceivableAsync(pageIdx, size, RestaurantName, DriverName, beginTime, endTime, beginDate, endDate, RestaurantPayStatus, BalanceWayEnum);
                return PartialView("IndexPartial", model);
            }
            return View();
        }

        public async Task<IActionResult> ReceivableSummary(int pageIdx, int size, DateTime? beginTime, DateTime? endTime)
        {
            if (pageIdx == 0)
                pageIdx = 1;

            if (size == 0)
                size = 20;
            var model = await _distributionDetailsAppService.GetReceivableSummaryListAsync(pageIdx, size, beginTime, endTime);

            if (Request.IsAjaxRequest())
            {
                return PartialView("ReceivableSummaryPartial", model);
            }
            return View(model);
        }

        public async Task<IActionResult> ExportExcel(DateTime? beginTime, DateTime? endTime, DateTime? beginDate, DateTime? endDate, string RestaurantName, string DriverName, RestaurantPayStatus? RestaurantPayStatus, BalanceWayEnum? BalanceWayEnum)
        {
            var list = await _distributionAppService.GetExportReceivableAsync(RestaurantName, DriverName, beginTime, endTime, beginDate, endDate, RestaurantPayStatus, BalanceWayEnum);
            return DoExcel<ReceivableNewOutputDto>(list, _env);
        }


        #region 制作收据
        public async Task<IActionResult> MakingReceipt(int? pageIdx, int size, DateTime? beginTime, DateTime? endTime, long? driverId, Guid? Line2, string keyword = "")
        {
            var pa = pageIdx ?? 1;

            if (size == 0)
                size = 20;

            var lines = await _distributionLineAppService.GetAllLineAsync();
            ViewBag.Line = lines.Select(o => new SelectListItem() { Value = o.Id.ToString(), Text = o.LineName });
            if (Request.IsAjaxRequest())
            {
                var data = await _distributionAppService.GetGenerateReceiptListAsync(pa, size, beginTime, endTime, keyword, driverId, Line2);
                return PartialView("MakingReceiptPartial", data);
            }


                return View();
        }


        public async Task<IActionResult> MakingReceiptDetails(Guid? driverLine, string ids, DateTime beginTime, DateTime endTime, int type = 0, long driverId = 0)
        {
            var data = await _distributionAppService.GetMakingReceiptsAsync(driverLine, ids, beginTime, endTime, type, driverId);
            var config = await _configInfoAppService.GetListAsync();
            ViewBag.Config = config;
            return View(data);
        }

        public async Task<JsonResult> ModifyMakingReceipt(ModifyReceiptDto input)
        {
            await _settlementInfoAppService.ModifyAsync(input);
            return Json("0");
        }

        #endregion

        #region 未制作收据餐厅
        public async Task<IActionResult> NotMadeReceipt(int? pageIdx, int size, DateTime? beginTime, DateTime? endTime, long? driverId, string keyword = "")
        {
            var pa = pageIdx ?? 1;

            if (size == 0)
                size = 20;

            if (Request.IsAjaxRequest())
            {
                var data = await _distributionAppService.GetGenerateReceiptListAsync(pa, size, beginTime, endTime, keyword, driverId, null);
                return PartialView("NotMadeReceiptPartial", data);
            }

            return View();
        }

        public async Task<IActionResult> ExportNotMadeReceipt(DateTime? beginTime, DateTime? endTime, long? driverId, string keyword = "")
        {
            var list = await _distributionAppService.GetExportGenerateReceiptListAsync(beginTime, endTime, keyword, driverId, null);
            return DoExcel<GenerateReceiptOutputDto>(list, _env);
        }

        #endregion

        #region 应收汇总

        public async Task<IActionResult> Summary(int? pageIdx, int size, DateTime? beginTime, DateTime? endTime, long? DriverId, Guid? Line, RestaurantPayStatus? RestaurantPayStatus, BalanceWayEnum? BalanceWayEnum, string keyword = "")
        {
            var pa = pageIdx ?? 1;
            if (size == 0)
                size = 10;

            if (Request.IsAjaxRequest())
            {

                var data = await _distributionAppService.GetSummaryReceivablePagesListAsync(pa, size, beginTime, endTime, DriverId, Line, RestaurantPayStatus, BalanceWayEnum, keyword);

                return PartialView("SummaryPartial", data);
            }

            var lines = await _distributionLineAppService.GetAllLineAsync();
            ViewBag.Line = lines.Select(o => new SelectListItem() { Value = o.Id.ToString(), Text = o.LineName });

            return View();

        }

        public async Task<IActionResult> ExportReceivableSummary(DateTime? beginTime, DateTime? endTime, long? DriverId, Guid? Line, RestaurantPayStatus? RestaurantPayStatus, BalanceWayEnum? BalanceWayEnum, string keyword = "")
        {
            var list = await _distributionAppService.GetExportSummaryReceivableListAsync(beginTime, endTime, DriverId, Line, RestaurantPayStatus, BalanceWayEnum, keyword);
            return DoExcel<SummaryReceivableOutputDto>(list, _env);
        }

        #endregion
    }
}